/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:hadss_uni_input/src/touch_finger.dart';
import 'dart:math' as math;
import 'package:vector_math/vector_math.dart' as vector;

/// 计算旋转的角度
double angleBetweenLines(TouchFinger f, TouchFinger s) {
  double startAngle = math.atan2(
      f.startOffset.dy - s.startOffset.dy, f.startOffset.dx - s.startOffset.dx);
  double endAngle = math.atan2(f.currentOffset.dy - s.currentOffset.dy,
      f.currentOffset.dx - s.currentOffset.dx);

  double angle = vector.degrees(startAngle - endAngle) % 360;
  if (angle < -180.0) angle += 360.0;
  if (angle > 180.0) angle -= 360.0;
  return angle;
}

/// 计算速度，distance 单位 dp，返回值单位为 dp/s
double calculateSpeed(DateTime startTime, double distance) {
  final endTime = DateTime.now();
  // 计算时间差
  Duration difference = endTime.difference(startTime);
  // 获取毫秒数
  int milliseconds = difference.inMilliseconds;
  // 时间差可能为0，作为被除数会出现异常，这里直接返回0
  if (milliseconds == 0) return 0;
  // 因时间差系统转换方法是返回int，秒的话到个位，误差会太大，因此使用毫秒，来消减误差
  final speed = distance / milliseconds;
  // 速度*1000，确保返回的单位是dp/s
  return speed * 1000;
}

enum GestureState {
  pointerDown,
  panStart,
  panUpdate,
  pinchStart,
  pinchUpdate,
  rotateStart,
  rotateUpdate,
  contentMenu,
  unknown
}
